package com.he.controller;

import org.springframework.stereotype.Controller;
import com.he.pojo.AirQualityIndex;
import com.he.service.AirQualityIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Transactional
public class AirQualityIndexController  {

    @Autowired
    private AirQualityIndexService airQualityIndexService;

    @RequestMapping("index")
    public String index(Model model) {
        // 查询数据库将数据传到前端展示
        model.addAttribute("airQualityIndex",airQualityIndexService.findAirQualityIndex());
        return "index";
    }

    @RequestMapping("toAddData")
    public String toAddData() {
        // 查询数据库将数据传到前端展示
        return "addData";
    }
    @RequestMapping("addData")
    @ResponseBody
    public String addData(AirQualityIndex airQualityIndex) {
        // 插入数据库将数据传到前端展示
        if (airQualityIndexService.addAirQualityIndex(airQualityIndex) > 0) {
            return "插入成功";
        }
        return "添加失败";
    }

    /**
     * 通过id查询数据回显到修改界面
     */
    @RequestMapping("toUpdateDate")
    public String toUpdateDate(Integer id, Model model) {
        model.addAttribute("air",airQualityIndexService.findAirQualityIndexById(id));
        return "updateDate";
    }

    /**
     * 修改数据
     */
    @RequestMapping("updateDate")
    @ResponseBody
    public String updateDate(AirQualityIndex airQualityIndex) {
        System.out.println(airQualityIndex);
        // 修改数据
        if (airQualityIndexService.updateData(airQualityIndex) > 0) {
            return "更新成功";
        }
        return "更新失败";
    }

    /**
     * 根据区域查询数据
     * @param district 区域
     * @return 数据
     */
    @RequestMapping("selectByDistrict")
    public String selectByDistrict(String district,Model model) {
        model.addAttribute("airQualityIndex",airQualityIndexService.selectByDistrict(district));
        return "index";
    }


}
